﻿using pdftron.PDF;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace CompleteReader.Flyouts
{
    public sealed partial class ViewModeControl : UserControl
    {
        public pdftron.PDF.PDFViewCtrl PDFViewCtrl { get; set; }

        private PDFViewCtrlPagePresentationMode _CurrentViewMode = PDFViewCtrlPagePresentationMode.e_single_page;
        private Button _CurrentViewModeButton;

        public delegate void PresentationModeChangedHandler();
        public event PresentationModeChangedHandler PresentationModeChanged = delegate { };

        public delegate void NightModeChangedHandler();
        public event NightModeChangedHandler NightModeChanged = delegate { };

        public delegate void RotateClockWiseHandler();
        public event RotateClockWiseHandler RotateClockwiseRequested = delegate { };

        public delegate void ThumbnailViewHandler();
        public event ThumbnailViewHandler ThumbnailViewRequested = delegate { };

        public ViewModeControl()
        {
            this.InitializeComponent();
            this.Loaded += QuickSettingsControl_Loaded;

        }

        private void QuickSettingsControl_Loaded(object sender, RoutedEventArgs e)
        {
            _CurrentViewMode = Settings.Settings.PagePresentationMode;
            ResolveLayoutButtonStates();
            ResolveNightMode();
        }

        private void ResolveLayoutButtonStates()
        {
            Button selectedButton = null;
            switch (_CurrentViewMode)
            {
                case PDFViewCtrlPagePresentationMode.e_single_page:
                    selectedButton = SinglePageButton;
                    break;
                case PDFViewCtrlPagePresentationMode.e_single_continuous:
                    selectedButton = ContinuousButton;
                    break;
                case PDFViewCtrlPagePresentationMode.e_facing:
                    selectedButton = FacingButton;
                    break;
                case PDFViewCtrlPagePresentationMode.e_facing_continuous:
                    selectedButton = FacingContinuousButton;
                    break;
                case PDFViewCtrlPagePresentationMode.e_facing_cover:
                    selectedButton = FacingCoverButton;
                    break;
                case PDFViewCtrlPagePresentationMode.e_facing_continuous_cover:
                    selectedButton = FacingContinuousCoverButton;
                    break;
            }

            if (selectedButton != null)
            {
                if (_CurrentViewModeButton != null)
                {
                    if (selectedButton != _CurrentViewModeButton)
                    {
                        _CurrentViewModeButton.IsEnabled = true;
                    }
                }
                _CurrentViewModeButton = selectedButton;
                selectedButton.IsEnabled = false;
            }
        }

        private void ResolveNightMode()
        {
            if (Settings.Settings.NightMode)
            {
                NightModeButton.Background = Application.Current.Resources["ThemeBrushHighlightLight"] as SolidColorBrush;
                NightModeButton.Foreground = new SolidColorBrush(Colors.White);
            }
            else
            {
                NightModeButton.Background = new SolidColorBrush(Colors.Transparent);
                NightModeButton.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        private void PageLayoutButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                string tag = button.Tag as string;
                if (tag != null)
                {
                    _CurrentViewMode = GetPresentationModeFromString(tag);
                    ResolveLayoutButtonStates();
                    Settings.Settings.PagePresentationMode = _CurrentViewMode;
                    PresentationModeChanged();
                }
            }
        }

        private PDFViewCtrlPagePresentationMode GetPresentationModeFromString(string str)
        {
            PDFViewCtrlPagePresentationMode newMode = PDFViewCtrlPagePresentationMode.e_single_page;
            if (str.Equals("s", StringComparison.OrdinalIgnoreCase))
            {
                newMode = PDFViewCtrlPagePresentationMode.e_single_page;
            }
            else if (str.Equals("scon", StringComparison.OrdinalIgnoreCase))
            {
                newMode = PDFViewCtrlPagePresentationMode.e_single_continuous;
            }
            else if (str.Equals("f", StringComparison.OrdinalIgnoreCase))
            {
                newMode = PDFViewCtrlPagePresentationMode.e_facing;
            }
            else if (str.Equals("fcon", StringComparison.OrdinalIgnoreCase))
            {
                newMode = PDFViewCtrlPagePresentationMode.e_facing_continuous;
            }
            else if (str.Equals("fcov", StringComparison.OrdinalIgnoreCase))
            {
                newMode = PDFViewCtrlPagePresentationMode.e_facing_cover;
            }
            else if (str.Equals("fconcov", StringComparison.OrdinalIgnoreCase))
            {
                newMode = PDFViewCtrlPagePresentationMode.e_facing_continuous_cover;
            }
            return newMode;
        }

        private void NightmodeButton_Click(object sender, RoutedEventArgs e)
        {
            Settings.Settings.NightMode = !Settings.Settings.NightMode;
            ResolveNightMode();
            NightModeChanged();
        }


        private void RotateClockwise_Click(object sender, RoutedEventArgs e)
        {
            RotateClockwiseRequested();
        }

        private void ThumbnailView_Click(object sender, RoutedEventArgs e)
        {
            ThumbnailViewRequested();
        }



    }
}
